﻿Public Class frmUnitofmeasure
    Dim Strsql As String
    Dim Mysql1 As New ClsSQLhelper
    Dim On_Load As Boolean = True
#Region "Function"
    Function Load_ComboboxData_site() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_site = False

            Strsql = "select * from site"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_site", "site")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSite
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "site"
                .ValueMember = "siteid"
                .SelectionStart = 0
                If My.Settings.CurrentSite <> "" Then
                    .SelectedValue = My.Settings.CurrentSite
                End If
            End With
            Load_ComboboxData_site = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_site - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function
    Sub Load_Data()
        Dim Strsql As String
        Dim DS As New DataSet
        Dim Mysql1 As New ClsSQLhelper
        Strsql = "select unitofmeasureid,unitofmeasure,note,active from itemunitofmeasure where active = 1 and siteid = " & Me.CboSite.SelectedValue & " order by unitofmeasure "
        DS = Mysql1.GetMYSQLDataset(Strsql, "DS_unitofmeasure", "unitofmeasure")
        Pn_Framework.Set_GridSetting(gridunitofmeasure, DS)
        On_Load = False
    End Sub
#End Region

    Private Sub frmUnitofmeasure_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ComboboxData_site()
        If My.Settings.CurrentuserGroup <> 1 Then Me.CboSite.Enabled = False
        Load_Data()
    End Sub
    Private Sub unitofmeasure_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridunitofmeasure.CellClick
        Try
            With gridunitofmeasure
                If .RowCount = 0 Then Exit Sub
                Me.Txtid.Text = .Item(0, e.RowIndex).Value.ToString
                Me.Txtname.Text = .Item(1, e.RowIndex).Value.ToString
                Me.TxtNote.Text = .Item(2, e.RowIndex).Value.ToString
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

            Dim Strsql As String
            Dim Mysql1 As New ClsSQLhelper
            Strsql = "Delete from itemunitofmeasure where unitofmeasureid = " & Me.Txtid.Text
            'Debug.Print(Strsql)
            Mysql1.MySQLExecute(Strsql)
            Me.Txtid.Text = ""
            Me.Txtname.Text = ""
            'Refresh Data
            Load_Data()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.Txtname.Text.Trim = "" Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Dim Strsql As String
            Dim Mysql1 As New ClsSQLhelper
            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                Strsql = "Select * from itemunitofmeasure where unitofmeasure= '" & Me.Txtname.Text & "' and siteid = " & Me.CboSite.SelectedValue
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_unitofmeasure", "unitofmeasure")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("Category นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into itemunitofmeasure (unitofmeasure,note,active,siteid) values ('" & Me.Txtname.Text & "', '" & Me.TxtNote.Text & "'," & Me.ChkActive.Checked & "," & Me.CboSite.SelectedValue & ")"
                Debug.Print(Strsql)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update itemunitofmeasure set unitofmeasure= '" & Me.Txtname.Text & "', note = '" & Me.TxtNote.Text & "',active =  " & Me.ChkActive.Checked
                Strsql = Strsql & " where unitofmeasureid = " & Me.Txtid.Text.Trim
                Debug.Print(Strsql)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            End If
            'Refresh Data
            Load_Data()
            BtnNew_Click(sender, e)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        Me.Txtid.Text = ""
        Me.Txtname.Text = ""
        Me.TxtNote.Text = ""
    End Sub

    Private Sub BtnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()

    End Sub
End Class